/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package monitoring.ws;

import com.sun.xml.bind.util.ListImpl;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import monitoring.beans.util.FileBean;
import monitoring.beans.util.FilterBean;
import monitoring.beans.util.ImportMessage;
import monitoring.logic.Import;
import monitoring.logic.MainTest;
import org.codehaus.jettison.json.JSONException;

/**
 * REST Web Service
 *
 * @author Regulus
 */
@Path("import")
public class ImportWS {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of ExportWS
     */
    public ImportWS() {
    }

    @GET
    @Path("/monitoring/{jsonParam}")
    @Produces("application/json")
    public String javajson(@PathParam("jsonParam") String jsonParam) throws JSONException {
        Import i = new Import();
        List<ImportMessage> listI = null;
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.alias("file", FileBean.class);
        FileBean f =  jsonParam==""?new FileBean():(FileBean)xstream.fromXML(jsonParam);                
        try {
            listI = i.import_(f);        
        } catch (Exception ex) {
            listI = new ArrayList<ImportMessage>();
            ImportMessage msg = new ImportMessage(ImportMessage.TYPE_PROBLEM, ex.getMessage(), "Planilha não importada",0,"Desconhecida");
            listI.add(msg);
            xstream.setMode(XStream.NO_REFERENCES);
            xstream.alias("messages", ImportMessage.class);
            ex.printStackTrace();
            return xstream.toXML(listI);
        }

        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("messages", ImportMessage.class);

        return xstream.toXML(listI);
    }

    @GET
    @Path("/imprimeJson/{jsonParam}")
    @Produces("application/json")
    public String javajjson(@PathParam("jsonParam") String jsonParam) throws JSONException {

        XStream xstream2 = new XStream(new JettisonMappedXmlDriver());
        xstream2.setMode(XStream.NO_REFERENCES);
        xstream2.alias("file", FileBean.class);
        FileBean f = new FileBean("tabelaII.xlsx");


        return xstream2.toXML(f);


    }
}
